﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager
{
    public class WebSystemMenuRepository : BaseRepository, IWebSystenMenuRepository
    {
        public WebSystemMenuRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }

        public List<WebSystemMenuPo> GetWebSystemMenuAll()
        {
            return _systemCenterDbSoaContext.WebSystemMenuPo.OrderBy(c => c.wm_order).ToList();
        }

        public WebSystemMenuPo GetWebSystemMenuByCode(string wMCode)
        {
            return _systemCenterDbSoaContext.WebSystemMenuPo.FirstOrDefault(c => c.wm_code == wMCode);
        }

        public List<WebSystemMenuPo> GetWebSystemMenusUserName(string userName)
        {
            return (from wsm in _systemCenterDbSoaContext.WebSystemMenuPo
                    join rmr in _systemCenterDbSoaContext.RoleModuleRelationPo on wsm.wm_module_code equals rmr.module_code
                    join aurr in _systemCenterDbSoaContext.AdminUserRoleRelationPo on rmr.role_id equals aurr.role_id
                    where aurr.user_name == userName
                    select wsm
                      ).ToList();
        }


        public WebSystemMenuDto GetWebSystemMenuViewByCode(string wMCode)
        {
            var view = (from sonWsm in _systemCenterDbSoaContext.WebSystemMenuPo
                        join parentWsm in _systemCenterDbSoaContext.WebSystemMenuPo  //pdm 代表父级模块
                        on sonWsm.wm_parent_code equals parentWsm.wm_code into leftJoinPwsm   //把前面的join into到 leftJoinPwsm
                        from leftJoinParent in leftJoinPwsm.DefaultIfEmpty()   //把join查询一下, 如果是有null的, 就默认返回为null
                        where sonWsm.wm_code == wMCode
                        select new { pname = leftJoinParent.wm_name, sonWsm }).FirstOrDefault();
            if (view == null) return null;
            WebSystemMenuDto wsmView = ModelCopier.CreateInstanceFrom<WebSystemMenuDto>(view.sonWsm);
            wsmView.ParentName = view.pname;
            return wsmView;
        }

        public bool WebSystemMenuAdd(WebSystemMenuPo wsm)
        {
            _systemCenterDbSoaContext.WebSystemMenuPo.Add(wsm);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }



        public bool WebSystemMenuUpdate(WebSystemMenuPo wsm)
        {
            WebSystemMenuPo db = _systemCenterDbSoaContext.WebSystemMenuPo.FirstOrDefault(c => c.wm_code == wsm.wm_code);
            ModelCopier.CopyModel(wsm, db, "creator_name", "creator_date");
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        
        public bool WebSystemMenuDelete(string wMCode)
        {
            var list = _systemCenterDbSoaContext.WebSystemMenuPo.FirstOrDefault(p => p.wm_code == wMCode);
            _systemCenterDbSoaContext.WebSystemMenuPo.Remove(list);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }


    }
}
